<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;

class AdminPower extends Base
{
    /**
     * 添加权限
     */
    public function powerAdd($data=array()){
        try {
            //添加权限数据
            if(!empty($data)){
                $seePower = $this->where([
                    ['navid','=',$data['navid']],['powertype','=',0]
                ])->first();
                if(!$seePower){ //自动添加查看权限
                    $nav = AdminNav::find($data['navid']);
                    $this->powername = "列表查看";
                    $this->navid = $data['navid'];
                    $this->path = $nav['navaction'].'show';
                    $this->powertype = 0;
                    $this->save();
                }
                $res = $this->firstOrCreate($data);
            }else{
                $res = $this->save();
            }
            if($res){
                return [
                    'code'=>0,
                    'message'=>''
                ];
            }else{
                return [
                    'code'=>20002,
                    'message'=>'添加失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //根据权限ID获取权限名称和模块名称
    public function getPowerName($ids=array()){
        try {
            $result = DB::table('admin_powers')->leftJoin('admin_navs','admin_powers.navid','=','admin_navs.id')
                ->leftJoin('admin_modules','admin_modules.id','=','admin_navs.moid')
                ->select('admin_navs.moid','admin_navs.id as navid','admin_modules.modspace','admin_modules.modname','admin_navs.navname','admin_navs.navaction')
                ->whereIn('admin_powers.id',$ids)->groupBy('admin_powers.navid')->get();
            $return = array();
            foreach ($result as $item){
                $return[$item->modspace][]  = $item->navaction;
            }
            return [
                'code'=>0,
                'data'=>$return
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }

    }

    /**
     * @param array $options  分页参数，含limit(每页数据条数)和page（当前页）
     * @param array $where  分页条件
     */
    public function getPageList($options=[],$where=[],$whereDates){
        try{
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = DB::table('admin_powers')->leftJoin('admin_navs','admin_navs.id','=','admin_powers.navid');
            if(!empty($whereDates)){
                $model = $model->whereDate('admin_powers.created_at','>=',$whereDates[0])->whereDate('admin_powers.created_at','<',$whereDates[1]);
            }
            $model = $model->where($where)->select('admin_powers.*','admin_navs.navname')
                    ->orderBy('navid','desc')->orderBy('powertype');
            $data = $this->paging($model,$page,$limit);
            return $data;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    /**
     * 编辑权限
     */
    public function doUpdate($data=array()){
        try {
            //存储数据
            $model = self::find($data['id']);
            $model->powername = $data['powername'];
            $model->path = $data['path'];
            $model->navid = $data['navid'];
            $model->powertype = $data['powertype'];
            $res = $model->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>[]
                ];
            }else{
                return [
                    'code'=>20002,
                    'message'=>'添加失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //获取所有菜单及其权限列表
    public function getNavPowers(){
        try {
            $data = (new AdminNav())->whereNotIn('id',[1,2,3])->select('id','navname')->get();
            foreach ($data as &$nav){
                $nav->powers = (new AdminPower())->where('navid','=',$nav->id)->select('id','powername')->orderBy('powertype')->get();
            }
            return [
              'code'=>0,
              'data'=>$data
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //获根据角色ID权限列表
    public function getRolePowers($roleid){
        try {
            if($roleid){
                $data = (new RolePower())->where('roleid','=',$roleid)->pluck('pid');
            }else {
                $data = (new AdminPower())->pluck('id');
            }
            return [
                'code'=>0,
                'data'=>$data
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }
}
